<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command\kuaishou\timeframe;

use app\command\BaseWork;
use app\enum\ConfigEnum;
use app\enum\KuaiShouEnum;
use app\model\mysql\KsTimeFrameTaskModel;
use app\service\batch\kuaishou\UnitTimeFrameService;
use app\service\batch\KuaiShouBatchService;
use think\console\Input;
use think\console\Output;
use think\facade\Cache;

class UnitTimeFrameTaskListWork extends BaseWork
{
    protected function configure()
    {
        // 指令配置
        $this->setName('ks_unit_time_frame_task_list_work');
        $this->setDescription('快手批量组投放时段修改任务入队');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        ini_set('memory_limit', '-1');
        $this->getOutputMessage('快手批量组投放时段修改任务入队开始');
        $st = time() + 300;
        $redis = Cache::store('redis')->handler();
        (new KsTimeFrameTaskModel())->where('status', KsTimeFrameTaskModel::STATUS_WAIT)
            ->where('is_pull', KsTimeFrameTaskModel::PULL_NOT)
            ->chunk(100, function ($items) use ($st, $redis) {
                $key = ConfigEnum::CACHE_CONFIG[KuaiShouEnum::BATCH_CONFIG_KEY]['unit_frame_time'];
                if ($items) {
                    foreach ($items as $item) {
                        if ($item['id']) {
                            $redis->rPush($key, $item['id']);
                            $this->getOutputMessage('快手批量组投放时段修改任务入队[' . $item['id'] . ']成功');
                        }
                    }
                } else {
                    return false;
                }
                if ($st <= time() - 300) {
                    return false;
                }
            }, 'id', 'DESC');
        $this->getOutputMessage('快手批量组投放时段修改任务入队结束');
    }
}
